from pylab import *
from numpy import * #unnecessary, just for pydev auto-complete
from multiprocessing import Pool
class MatchingPursuit():
    def __init__(self, length, startingFreq, numFreq, numFreqPerOctave):
        self.init_basis(length, startingFreq, numFreq, numFreqPerOctave)
        
    def init_basis(self, length, startingFreq, numFreq, numFreqPerOctave):
        self.frequency = startingFreq * exp(log(2) * arange(0, numFreq) / numFreqPerOctave)#Todo logspace
        self.angular_frequency = 2 * pi * self.frequency
        t = linspace(0, 1, length, False)[:, newaxis]
        tmp = map(lambda af : hstack([(1 - t) * cos(af * t), t * cos(af * t), (1 - t) * sin(af * t), t * sin(af * t)]), self.angular_frequency)
        self.basis = vstack(map(pinv, tmp))
        
    def matching_pursuit(self, data):
        tmp = dot(self.basis, asarray(data))
        
        
#        I = tmp.argmax()
#        print I
#        A = -tmp.item(I)
#        w = self.angular_frequency[I]
#        p = arctan(-c.item(I) / s.item(I))
#        return array([A, w, p])

if __name__ == '__main__':
    tmp = MatchingPursuit(44100 / 10, 12.5, 1200, 120)
    data = random.random((44100 / 10, 1))
    data = ones((44100 / 10, 1))
    t = linspace(0, 1, 44100 / 10, False)
    data = t*sin(500 * 2 * pi * t)
    print tmp.matching_pursuit(data)
